package org.jawa.filter;

import org.apache.commons.lang.StringUtils;
import org.jawa.Constants;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.jstl.core.Config;

import java.io.IOException;
import java.util.Locale;

/**
 * Filter to wrap request with a request including user preferred locale.
 */
public class LocaleFilter extends OncePerRequestFilter {

	public void doFilterInternal(HttpServletRequest request,
			HttpServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpSession session = request.getSession(true);

		Locale locale = Constants.getLocale();
		//如果设置了LOCAL，设将LOCAL设置为用户指定的LOCAL
		if(locale!=null){
			 Config.set(session, Config.FMT_LOCALE, locale);
		}

		chain.doFilter(request, response);
		//        
		// // Reset thread-bound LocaleContext.
		LocaleContextHolder.setLocaleContext(null);
	}
}
